home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / CH5 / EMAGA5 BOOK CODE / control / server / server.cs
Text File  |  2006-09-24  |  10KB  |  291 lines

  1. //============================================================================
  2. // control/server/server.cs
  3. //
  4. //  server-side game specific module for 3D2E emaga5 sample game
  5. //  provides client connection management and and player/avatar spawning
  6. //
  7. //  Copyright (c) 2003,2006  by Kenneth C.  Finney.
  8. //============================================================================
  9.  
  10. //============================================================================
  11. // GameConnection Methods
  12. // Extensions to the GameConnection class. Here we add some methods
  13. // to handle player spawning and creation.
  14. //============================================================================
  15.  
  16. function OnServerCreated()
  17. //----------------------------------------------------------------------------
  18. // Once the engine has fired up the server, this function is called
  19. //----------------------------------------------------------------------------
  20. {
  21.    $Game::StartTime = 0;
  22.    
  23.    Exec("./misc/camera.cs");
  24.    Exec("./misc/shapeBase.cs");
  25.    Exec("./misc/item.cs");
  26.    Exec("./players/player.cs"); // Load the player datablocks and methods
  27.    Exec("./players/beast.cs"); // Load the player datablocks and methods
  28.    Exec("./players/ai.cs"); // Load the player datablocks and methods
  29.    Exec("./weapons/weapon.cs");
  30.    Exec("./weapons/crossbow.cs");
  31. }
  32.  
  33. function startGame()
  34. {
  35.    if ($Game::Duration) // Start the game timer
  36.       $Game::Schedule = schedule($Game::Duration * 1000, 0, "onGameDurationEnd" );
  37.    $Game::Running = true;
  38.    schedule( 2000, 0, "CreateBots");
  39. }
  40.  
  41. function onMissionLoaded()
  42. {
  43.    // Called by loadMission() once the mission is finished loading.
  44.    // Nothing special for now, just start up the game play.
  45.    startGame();
  46. }
  47.  
  48. function onMissionEnded()
  49. {
  50.    cancel($Game::Schedule);
  51.    $Game::Running = false;
  52. }
  53.  
  54. function GameConnection::OnClientEnterGame(%this)
  55. //----------------------------------------------------------------------------
  56. // Called when the client has been accepted into the game by the server.
  57. //----------------------------------------------------------------------------
  58. {
  59.    // Create a new camera object.
  60.    %this.camera = new Camera() {
  61.       dataBlock = Observer;
  62.    };
  63.    MissionCleanup.add( %this.camera );
  64.    %this.camera.scopeToClient(%this);
  65.  
  66.    // Create a player object.
  67.    %this.spawnPlayer();
  68. }
  69.  
  70. function GameConnection::SpawnPlayer(%this)
  71. //----------------------------------------------------------------------------
  72. // This is where we place the player spawn decision code.
  73. // It might also call a function that would figure out the spawn
  74. // point transforms by looking up spawn markers.
  75. // Once we know where the player will spawn, then we create the avatar.
  76. //----------------------------------------------------------------------------
  77. {
  78.  
  79.    %this.createPlayer("0 0 201 1 0 0 0");
  80. }
  81.  
  82. function GameConnection::CreatePlayer(%this, %spawnPoint)
  83. //----------------------------------------------------------------------------
  84. // Create the player's avatar object, set it up, and give the player control
  85. // of it.
  86. //----------------------------------------------------------------------------
  87. {
  88.    if (%this.player > 0)//The player should NOT already have an avatar object.
  89.    {                     // if he does, that's a Bad Thing.
  90.       Error( "Attempting to create an angus ghost!" );
  91.    }
  92.    // Create the player object
  93.    %player = new Player() {
  94.       dataBlock = MaleAvatar;   // defined in players/player.cs
  95.       client = %this;           // the avatar will have a pointer to its
  96.    };                           // owner's connection
  97.  
  98.    // Player setup...
  99.    %player.setTransform(%spawnPoint); // where to put it
  100.  
  101.    // Update the camera to start with the player
  102.    %this.camera.setTransform(%player.getEyeTransform());
  103.    %player.setEnergyLevel(100);
  104.  
  105.    // Give the client control of the player
  106.    %this.player = %player;
  107.    %this.setControlObject(%player);
  108. }
  109.  
  110. function GameConnection::onDeath(%this, %sourceObject, %sourceClient, %damageType, %damLoc)
  111. {
  112.  
  113.    // Switch the client over to the death cam and unhook the player object.
  114.    if (IsObject(%this.camera) && IsObject(%this.player))
  115.    {
  116.       %this.camera.setMode("Death",%this.player);
  117.       %this.setControlObject(%this.camera);
  118.    }
  119.    %this.player = 0;
  120.  
  121.    // Doll out points and display an appropriate message
  122.    if (%damageType $= "Suicide" || %sourceClient == %this)
  123.    {
  124.  
  125.    }
  126.    else
  127.    {
  128.    }
  129. }
  130.  
  131. function serverCmdToggleCamera(%client)
  132. //-----------------------------------------------------------------------------
  133. //
  134. //-----------------------------------------------------------------------------
  135. {
  136.    %co = %client.getControlObject();
  137.    if (%co == %client.player)
  138.    {
  139.       %co = %client.camera;
  140.       %co.mode = toggleCameraFly;
  141.    }
  142.    else
  143.    {
  144.       %co = %client.player;
  145.       %co.mode = observerFly;
  146.    }
  147.    %client.setControlObject(%co);
  148. }
  149.  
  150. function serverCmdDropPlayerAtCamera(%client)
  151. //-----------------------------------------------------------------------------
  152. //
  153. //-----------------------------------------------------------------------------
  154. {
  155.    if ($Server::DevMode || IsObject(EditorGui))
  156.    {
  157.       %client.player.setTransform(%client.camera.getTransform());
  158.       %client.player.setVelocity("0 0 0");
  159.       %client.setControlObject(%client.player);
  160.    }
  161. }
  162.  
  163. function serverCmdDropCameraAtPlayer(%client)
  164. //-----------------------------------------------------------------------------
  165. //
  166. //-----------------------------------------------------------------------------
  167. {
  168.    %client.camera.setTransform(%client.player.getEyeTransform());
  169.    %client.camera.setVelocity("0 0 0");
  170.    %client.setControlObject(%client.camera);
  171. }
  172.  
  173.  
  174. function serverCmdUse(%client,%data)
  175. //-----------------------------------------------------------------------------
  176. // Server Item Use
  177. //-----------------------------------------------------------------------------
  178. {
  179.    %client.getControlObject().use(%data);
  180. }
  181.  
  182. function centerPrintAll( %message, %time, %lines )
  183. //-----------------------------------------------------------------------------
  184. //
  185. //-----------------------------------------------------------------------------
  186. {
  187.    if( %lines $= "" || ((%lines > 3) || (%lines < 1)) )
  188.       %lines = 1;
  189.  
  190.    %count = ClientGroup.getCount();
  191.    for (%i = 0; %i < %count; %i++)
  192.     {
  193.         %cl = ClientGroup.getObject(%i);
  194.       if( !%cl.isAIControlled() )
  195.          commandToClient( %cl, 'centerPrint', %message, %time, %lines );
  196.    }
  197. }
  198.  
  199. function bottomPrintAll( %message, %time, %lines )
  200. //-----------------------------------------------------------------------------
  201. //
  202. //-----------------------------------------------------------------------------
  203. {
  204.    if( %lines $= "" || ((%lines > 3) || (%lines < 1)) )
  205.       %lines = 1;
  206.  
  207.    %count = ClientGroup.getCount();
  208.     for (%i = 0; %i < %count; %i++)
  209.     {
  210.         %cl = ClientGroup.getObject(%i);
  211.       if( !%cl.isAIControlled() )
  212.          commandToClient( %cl, 'bottomPrint', %message, %time, %lines );
  213.    }
  214. }
  215.  
  216. //-------------------------------------------------------------------------------------------------------
  217.  
  218. function centerPrint( %client, %message, %time, %lines )
  219. //-----------------------------------------------------------------------------
  220. //
  221. //-----------------------------------------------------------------------------
  222. {
  223.    if( %lines $= "" || ((%lines > 3) || (%lines < 1)) )
  224.       %lines = 1;
  225.  
  226.  
  227.    commandToClient( %client, 'CenterPrint', %message, %time, %lines );
  228. }
  229.  
  230. function bottomPrint( %client, %message, %time, %lines )
  231. //-----------------------------------------------------------------------------
  232. //
  233. //-----------------------------------------------------------------------------
  234. {
  235.    if( %lines $= "" || ((%lines > 3) || (%lines < 1)) )
  236.       %lines = 1;
  237.  
  238.    commandToClient( %client, 'BottomPrint', %message, %time, %lines );
  239. }
  240.  
  241. //-------------------------------------------------------------------------------------------------------
  242.  
  243. function clearCenterPrint( %client )
  244. //-----------------------------------------------------------------------------
  245. //
  246. //-----------------------------------------------------------------------------
  247. {
  248.    commandToClient( %client, 'ClearCenterPrint');
  249. }
  250.  
  251. function clearBottomPrint( %client )
  252. //-----------------------------------------------------------------------------
  253. //
  254. //-----------------------------------------------------------------------------
  255. {
  256.    commandToClient( %client, 'ClearBottomPrint');
  257. }
  258.  
  259. //-------------------------------------------------------------------------------------------------------
  260.  
  261. function clearCenterPrintAll()
  262. //-----------------------------------------------------------------------------
  263. //
  264. //-----------------------------------------------------------------------------
  265. {
  266.     %count = ClientGroup.getCount();
  267.     for (%i = 0; %i < %count; %i++)
  268.     {
  269.         %cl = ClientGroup.getObject(%i);
  270.       if( !%cl.isAIControlled() )
  271.          commandToClient( %cl, 'ClearCenterPrint');
  272.    }
  273. }
  274.  
  275. function clearBottomPrintAll()
  276. //-----------------------------------------------------------------------------
  277. //
  278. //-----------------------------------------------------------------------------
  279. {
  280.     %count = ClientGroup.getCount();
  281.     for (%i = 0; %i < %count; %i++)
  282.     {
  283.         %cl = ClientGroup.getObject(%i);
  284.       if( !%cl.isAIControlled() )
  285.          commandToClient( %cl, 'ClearBottomPrint');
  286.    }
  287. }
  288.  
  289. function onNeedRelight() // Stub routine to reduce console spam
  290. {
  291. }